package com.muchfish.reactor.api;

import reactor.core.publisher.Flux;
import reactor.core.publisher.GroupedFlux;

public class ReactorGroupByExample {
    public static void main(String[] args) {
        Flux<Integer> source = Flux.range(1, 10);

        // 将数据流中的元素按奇偶分组
        Flux<GroupedFlux<String, Integer>> groupedFlux = source.groupBy(value -> value % 2 == 0 ? "Even" : "Odd");

        groupedFlux.subscribe(group -> {
            String key = group.key();
            group.subscribe(value -> System.out.println(key + ": " + value));
        });
    }
}
